using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class asp_pages_PetManager : System.Web.UI.Page
{
    PetCategoryDAO daoPetCate = new PetCategoryDAO();
    PetDAO daoPet = new PetDAO();
    Pet currentPet ;
    public void LoadInitData()
    {

        int id = int.Parse(Request["pet_id"]);
         currentPet = daoPet.GetPet(id);
        if (currentPet.ID == -1) return;
        if (!IsPostBack)
        {
            cmbPetCategories.DataSource = daoPetCate.GetAllCategories();
            cmbPetCategories.DataTextField = "Name";
            cmbPetCategories.DataValueField = "ID";
            cmbPetCategories.DataBind();
        }
        txtName.Text = currentPet.Name;
        txtHeight.Text = currentPet.Height.ToString();
        txtWeight.Text = currentPet.Weight.ToString();
        int index=0;
        foreach (ListItem item in cmbPetCategories.Items)
        {
            if (item.Value == currentPet.Category.ID.ToString()) break;
            index++;
        }
        cmbPetCategories.SelectedIndex = index;
        txtPrice.Text = currentPet.Price.ToString();
        txtAvailableNumber.Text = currentPet.Quantity.ToString();
        //fupImageURL.T
        fckEditor.Value = currentPet.Information;
        imgPet.ImageUrl = currentPet.ImageURL;
        lblPreviewContent.Text = currentPet.Information;
        
    }

    protected void Page_Load(object sender, EventArgs e)
    {        
        LoadInitData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblPreviewContent.Text = fckEditor.Value;
    }
    protected void lnkEditInfor_Click(object sender, EventArgs e)
    {

    }
    protected void Button3_Click(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Validate();
        if (!IsValid) return;
        //if (fupImageURL.FileContent.Length > 2000) { return; }
        string path = @"../imageLib/";
        Pet pet = new Pet();
        pet.ID = currentPet.ID;
        pet.Name = txtName.Text;
        pet.Height = float.Parse(txtHeight.Text);
        pet.Weight = float.Parse(txtWeight.Text);
        pet.Category.ID = int.Parse(cmbPetCategories.SelectedValue);
        pet.ImageURL =path+fupImageURL.FileName;
        pet.Price = decimal.Parse(txtPrice.Text);
        pet.TypeID = 1;//is pet
        if (daoPet.UpdatePet(pet) > 0)
        {
            lblMessage.Text = "Information has been updated successfully";
            fupImageURL.SaveAs(Server.MapPath(path) + fupImageURL.FileName);
            try
            {
                System.IO.File.Delete(Server.MapPath(currentPet.ImageURL));
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Information is updated but error occured when try to delete old file";
            }
            
            
        }
        else
        {
            lblMessage.Text = "Sorry. Error has occured and the information is not added to database.";
        }
    }
    protected void btnPrevInPage_Click(object sender, EventArgs e)
    {
       
    }
}
